package com.qls.web.modules.dt.controller;

import com.qls.basic.api.ConsumerRemoteService;
import com.qls.core.constant.Constant;
import com.qls.core.entity.DtStore;
import com.qls.core.utils.R;
import com.qls.redis.RedisUtil;
import com.qls.web.common.utils.PageUtils;
import com.qls.web.modules.dt.service.StoreService;
import com.qls.web.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


/**
 * 分销商店铺
 *
 * @author yj
 * @email yj@gmail.com
 * @date 2021-09-04 11:21:52
 */
@RestController
@RequestMapping("dt/store")
public class StoreController extends AbstractController {
    @Autowired
    private StoreService storeService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ConsumerRemoteService consumerRemoteService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dt:store:list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("dtId", getUserDtId());
        PageUtils page = storeService.sqlQueryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dt:store:info")
    public R info(@PathVariable("id") Long id) {
        DtStore store = storeService.getById(id);

        return R.ok().put("store", store);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dt:store:save")
    public R save(@RequestBody DtStore store) {
        storeService.save(store);
        String id = store.getId().toString();
        redisUtil.hashSet(Constant.Redis.STOREID_USER_STORE.getValue(), id, store);
        Map<String, Object> map = new HashMap<>();
        map.put("storeId", id);
        consumerRemoteService.create(map);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dt:store:update")
    public R update(@RequestBody DtStore store) {
        storeService.updateById(store);
        String id = store.getId().toString();
        redisUtil.hashSet(Constant.Redis.STOREID_USER_STORE.getValue(), id, store);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dt:store:delete")
    public R delete(@RequestBody Long[] ids) {
        storeService.removeByIds(Arrays.asList(ids));
        String[] strings = new String[ids.length];
        for (int i = 0; i < ids.length; i++) {
            String id = ids[i].toString();
            strings[i] = id;
            Map<String, Object> map = new HashMap<>();
            map.put("storeId", id);
            consumerRemoteService.destroy(map);
        }
        redisUtil.hashDel(Constant.Redis.STOREID_USER_STORE.getValue(), strings);
        return R.ok();
    }

}
